<?php
namespace App\Http\Controllers\Admin;

use App\Models\Menu;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

/**
 * 菜单
 *
 * @author Xiaole Chen
 *
 */
class MenuController extends Controller
{

    /**
     * 菜单列表
     */
    public function getList()
    {
        $menu = $this->getLists();
        return view('admin.menu.list')->withData($menu);
    }

    /**
     * 菜单导入列表
     */
    public function getLoadList()
    {

        // 取得系统内的所有权限。
        $actions = [];
        foreach (Route::getRoutes() as $route) {
            $name = $route->getName();
            $action = $route->getAction();
            if (!is_null($name) && strpos($name, '_') === false && in_array('auth.admin', $route->middleware())) {
                $actions[$name] = $action['alias'];
            }
        }
        $menu = Menu::latest('sort')->get()->toArray();
        foreach ($menu as $val) {
            unset($actions[$val['route']]);
        }

        return view('admin.menu.loadlist')
            ->withParent($this->getLists())
            ->withData($actions);
    }

    /**
     * 删除菜单
     */
    public function delete(Request $request)
    {
        Menu::where('id', $request->id)->first()->delete();
        return redirect()->back()->withMessageSuccess('成功删除菜单');
    }

    /**
     * 获取菜单详情
     */
    public function getEdit(Request $request)
    {
        $menu = Menu::find($request->id);
        $menu_list = $this->getLists();

        return view('admin.menu.edit')->withData($menu)
            ->withParent($menu_list);
    }

    /**
     * 菜单列表
     */
    protected function getLists($pid = 0, &$lists = array(), $deep = 1)
    {
        $data = Menu::where('pid', $pid)->orderBy('sort', 'desc')->get()->toArray();
        foreach ($data as $item) {
            $item['option'] = str_repeat('&nbsp;', $deep) . '|-' . $item['name'];
            $item['level'] = $deep;
            $lists[] = $item;
            $this->getLists($item['id'], $lists, $deep + 3); //进入子类之前深度+1         --$deep; //从子类退出之后深度-1     }
        }
        return $lists;
    }


    /**
     * 保存菜单
     */
    public function save(Request $request)
    {
        // 验证数据。
        $this->validate($request, [
            'name' => [
                'required'
            ]
        ]);
        $menu = $request->has('id') ? Menu::find($request->input('id')) : new Menu();
        $menu->name = $request->name;
        if ($request->has('pid')) {
            $menu->pid = $request->pid;
        }
        if ($request->has('route')) {
            $menu->route = $request->route;
        }
        if ($request->has('status')) {
            $menu->status = $request->status;
        }
        if ($request->has('sort')) {
            $menu->sort = $request->sort;
        }
        $menu->save();

        return redirect()->route('AdminMenuList');
    }

    /**
     *  保存导入菜单
     */
    public function saveLoad(Request $request)
    {
        $enable = $request->input('enable');
        $name = $request->input('name');
        $pid = $request->input('pid');
        $sort = $request->input('sort');
        $status = $request->input('status');

          if($enable){
              foreach($enable as $key=>$val){
                  $menu =  new Menu();
                  $menu->pid = $pid[$key];
                  $menu->name = $name[$key];
                  $menu->sort = $sort[$key];
                  $menu->status = $status[$key];
                  $menu->route = $val;
                  $menu->save();
              }
          }


        return redirect()->route('AdminMenuLoadList');
    }
}

